﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Commen;
using Code.Models;

namespace System.Collections.Generic
{
    public static class IEnumerableExtension
    {
        /// <summary>
        /// 返回一个 NameValue 对象
        /// </summary>
        /// <param name="keySelector">用于从每个对象中提取键的函数</param>
        /// <param name="elementSelector">用于从每个对象中提取值的函数</param>
        /// <exception cref="System.ArgumentNullException"/>
        public static NameValue<TKey, TElement> ToNameValue<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
        {
            if (source == null)
            {
                throw new ArgumentNullException ("source");
            }
            if (keySelector == null)
            {
                throw new ArgumentNullException("keySelector");
            }
            if (elementSelector == null)
            {
                throw new ArgumentNullException("elementSelector");
            }
            NameValue<TKey, TElement> rs = new NameValue<TKey, TElement>();
            foreach (TSource item in source)
            {
                rs.Add(keySelector(item), elementSelector(item));
            }
            return rs;
        }

        /// <summary>
        /// 返回一个 HashTable 对象
        /// </summary>
        /// <param name="keySelector">用于从每个对象中提取键的函数</param>
        /// <param name="elementSelector">用于从每个对象中提取值的函数</param>
        /// <exception cref="System.ArgumentNullException"/>
        public static HashTable<TKey, TElement> ToHashTable<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
        {
            if (source == null)
            {
                throw new ArgumentNullException ("source");
            }
            if (keySelector == null)
            {
                throw new ArgumentNullException("keySelector");
            }
            if (elementSelector == null)
            {
                throw new ArgumentNullException("elementSelector");
            }
            HashTable<TKey, TElement> rs = new HashTable<TKey, TElement>();
            foreach (TSource item in source)
            {
                rs.Add(keySelector(item), elementSelector(item));
            }
            return rs;
        }

        /// <summary>
        /// 遍历集合
        /// </summary>
        /// <param name="action">对于集合中每个元素的回调函数</param>
        /// <exception cref="System.ArgumentNullException"/>
        public static IEnumerable<TSource> Each<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            foreach (var item in source)
                action(item);

            return source;
        }

        /// <summary>
        /// 遍历集合
        /// </summary>
        /// <param name="action">对于集合中每个元素的回调函数</param>
        /// <exception cref="System.ArgumentNullException"/>
        public static IEnumerable<TSource> Each<TSource>(this IEnumerable<TSource> source, Action<TSource, int> action)
        { 
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            int i = 0;
            foreach (var item in source)
                action(item, i++);

            return source;
        }

    }
}
